var async = require("async")
var util = require("../../../util/util.js")
module.exports = function(app) {
	return new DBRemote(app);
};

var DBRemote = function(app) {
	this.app = app
	DBRemote.app = app
    DBRemote.dbService = this.app.get("dbService")
}

var local = {}

//获取背包
DBRemote.prototype.getBag = function(uid,cb){
	DBRemote.dbService.db.hgetall("game:bagList:"+uid,function(err,data) {
		if(err || !data){
			cb(true,{})
		}else{
			cb(true,data)
		}
	})
}

//使用道具
DBRemote.prototype.useItem = function(uid,itemId,cb) {
	DBRemote.dbService.addItem(uid,itemId,-1,function(flag,curValue) {
		if(flag && curValue >= 0){
			local.useItem(uid,itemId)
			cb(true)
		}else{
			DBRemote.dbService.addItem(uid,itemId,1)
			cb(false)
		}
	})
}
//物品使用效果
local.useItem = function(uid,itemId) {
	switch(itemId){
		case 1001 :
			DBRemote.dbService.addItemReason(uid,202,1,"闯关卡")
		break
		case 1002 :
			DBRemote.dbService.db.hget("NoAdTimeList",uid,function(err,data) {
				var value = (new Date()).getTime()
				if(!err && data){
					if(data > value){
						value = data
					}
				}
				value = parseInt(value)
				value += 7 * 24 * 60 * 60 * 1000
				// value += 5 * 60 * 1000
				DBRemote.dbService.db.hset("NoAdTimeList",uid,value)
			})
		break
		default:
			return
	}
}